<?php
namespace app\index\controller;

use app\index\model\Fiction;
use QL\QueryList;

class Fictions extends Index
{
    private $query_list = '../extend/query/QueryList.php';
    private $php_query = '../extend/query/phpQuery.php';
    public function index()
    {
        $url = 'http://www.126shu.com/1693/';
        $rule = [
            'chapter_link' => ['#list dl dd a','href'],
            'chapter_title' => ['#list dl dd a','text'],
        ];
        $res = $this->get_data($url,$rule,'UTF-8','gbk');
        foreach($res as $key => $value)
        {
            $model = new Fiction();
            $time = date('Y-m-d H:i:s');
            $data = [
                'p_id' => 0,
                'title' => $value['chapter_title'],
                'link' => $value['chapter_link'],
                'save' => 'false',
                'create_time' => $time,
                'update_time' => $time,
            ];
            $model->data = $data;
            $model->add();
//            $re = $this->get_data('http://www.biquge.cm'.$value['chapter_link'],$detail_rules,'UTF-8','gbk');
//            file_put_contents($save_path.'/'.$value['chapter_title'].'.txt' ,$re[0]['text']);
        }
        print_r('The end');
    }
    public function chapter()
    {
        ini_set('max_execution_time',0);
        $model = new Fiction();
        $count = $model->get_count();
        $save_path = 'static/fiction/chapter/hc';
        $detail_rules = [
            'text' => ['#content','text']
        ];
        for($i = 1;$i<= $count;$i++)
        {
            $model = new Fiction();
            $model->map['ID'] = $i;
            $res = $model->get_one();
            if($res->save == 'false')
            {
                $text = $this->get_data('http://www.126shu.com'.$res->link,$detail_rules,'UTF-8','gbk')[0]['text'];
                $text = str_replace('&#160;','',$text);
                $text = str_replace('()','',$text);
                $text = explode('闪婚总裁契约妻、',$text)[1];
                $text = explode('最新全本：',$text)[0];
                file_put_contents($save_path.'/'.$res->title.'.txt' ,$text);
                $model->data = [
                    'save' => 'true',
                    'text' => $text,
                ];
                $model->modify();
                usleep(200);
//                print_r($text);die;
            }
            else
            {
//                print_r('skip');
            }
        }
    }
    public function compound()
    {
        ini_set('max_execution_time',0);
        $model = new Fiction();
        $count = $model->get_count();
        $save_path = 'static/fiction/chapter';
        for($i = 1;$i<= $count;$i++)
        {
            $model = new Fiction();
            $model->map['ID'] = $i;
            $res = $model->get_one();
            $text = $res->title."\n".$res->text;
            if($i === 1)
                file_put_contents($save_path.'/111.txt',$text);
            else
                file_put_contents($save_path.'/111.txt',$text,FILE_APPEND);
        }
    }
    public function get_data($url = '' , $rules = [] , $outputEncoding = null, $inputEncoding = null)
    {
        include_once $this->query_list;
        include_once $this->php_query;
        $html = curl_get($url);
        return QueryList::Query($html,$rules,'',$outputEncoding, $inputEncoding)->data;
    }
}